﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MessageWorkflow;
using SimpleConsoleExamples.Examples;
using System.Reflection;

namespace SimpleConsoleExamples
{
    class Program
    {
        static void Main(string[] args)
        {

            Dictionary<string, IExample> examples = new Dictionary<string, IExample>();

            var targetType = typeof(IExample);
            var foundTypes = AppDomain.CurrentDomain.GetAssemblies()
                                                    .ToList()
                                                    .SelectMany(s => s.GetTypes())
                                                    .Where(p => targetType.IsAssignableFrom(p))
                                                    .Where(p => p.IsClass)
                                                    .OrderBy(x => x.Name);

            foreach (var foundType in foundTypes)
            {
                IExample entry = (IExample)Activator.CreateInstance(foundType);
                string name = foundType.Name;

                examples[name] = entry;
            }

            string input;

            while (true)
            {
                Console.WriteLine("Choose your example (or x to quit):\n\n");

                int index = 0;

                foreach (var entry in examples)
                {
                    index++;
                    Console.WriteLine("{0}: {1}", index, entry.Key);
                }

                Console.Write("\n\nEnter: ");
                input = Console.ReadLine().ToLower();

                if (input == "x")
                {
                    break;
                }
                else
                {
                    index = 0;
                    IExample example = null;

                    foreach (var entry in examples)
                    {
                        index++;
                        if (index.ToString() == input)
                        {
                            example = entry.Value;
                        }
                    }

                    if (example == null)
                    {
                        Console.Write("\n\nInvalid input\n\n");
                    }
                    else
                    {
                        example.Run();
                    }
                }
            }
        }
    }
}
